package com.fanghaoran.view.manager;

import com.fanghaoran.controller.ManagerController;
import com.fanghaoran.entity.Book;
import com.fanghaoran.view.user.BackGroundPanel;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * @Author: win11
 * @Date: 2023/03/28/15:31
 * @Description: 添加单词页面
 */
public class AddWordFrame extends JFrame {
    /**
     * 单词书名,默认四级书
     */
    private String bookName="CET-4";

    public AddWordFrame() throws Exception {
        //最外层界面frame的设置
        initFrame();
        //界面中的视图,控件等的设置
        initView();

        //设置可见
        this.setVisible(true);
    }

    private void initView() throws Exception {
        //panel来放组件
        BackGroundPanel panel = new BackGroundPanel(ImageIO.read(new File("src//image//rBackground.jpg")));
        //用垂直box实现组件垂直排列
        Box vBox = Box.createVerticalBox();
        vBox.add(Box.createVerticalStrut(100));

        Box box1 = Box.createHorizontalBox();
        //单词文本
        JLabel wordText = new JLabel("单词:");
        //单词输入框
        JTextField word = new JTextField(20);
        //添加到box
        box1.add(wordText);
        box1.add(Box.createHorizontalStrut(20));
        box1.add(word);


        Box box2 = Box.createHorizontalBox();
        //释义文本
        JLabel translationText = new JLabel("释义:");
        //释义输入框
        JTextField translation = new JTextField(20);
        //添加到box
        box2.add(translationText);
        box2.add(Box.createHorizontalStrut(20));
        box2.add(translation);


        Box box3 = Box.createHorizontalBox();
        JLabel addToText = new JLabel("操作书籍");
        //获取书籍数据
        @SuppressWarnings("unchecked")
        List<Book> books = (List<Book>) new ManagerController().getBookInfo().getMessage();
        //存到selectList选项集合
        List<String> selectList = new ArrayList<>();
        for (Book book : books) {
            selectList.add(book.getName());
        }
        //转成vector对象
        Vector<String> vector = new Vector<>(selectList);
        //选项列表
        JComboBox<String> selector = new JComboBox<>(vector);
        box3.add(addToText);
        box3.add(Box.createHorizontalStrut(20));
        box3.add(selector);
        //选项条目列表点击事件
        selector.addItemListener(e -> {
                    if (selector.getSelectedItem() != null) {
                        for (String s : selectList) {
                            if (selector.getSelectedItem().equals(s)) {
                                //确认当前要添加单词的单词书
                                bookName = s;
                            }
                        }
                    }
                }
        );

        Box box4 = Box.createHorizontalBox();
        //添加按钮
        JButton addBtn = new JButton("添加");
        box4.add(addBtn);
        //按钮添加点击事件
        addBtn.addActionListener(e -> {
                    String result = null;
                    try {
                        result = new ManagerController().insertWord(word.getText(), translation.getText(), bookName).getResult();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    //对返回结果进行图形化提示
                    if ("输入为空".equals(result)){
                        JOptionPane.showMessageDialog(panel, "输入不能为空");
                    }else if ("该单词在该单词书中已存在".equals(result)) {
                        JOptionPane.showMessageDialog(panel, "该单词在该单词书中已存在");
                    } else {
                        JOptionPane.showMessageDialog(panel, "添加成功");
                        this.dispose();
                    }
                }
        );

        vBox.add(box1);
        vBox.add(Box.createVerticalStrut(30));
        vBox.add(box2);
        vBox.add(Box.createVerticalStrut(30));
        vBox.add(box3);
        vBox.add(Box.createVerticalStrut(30));
        vBox.add(box4);

        panel.add(vBox);

        this.add(panel);
    }

    private void initFrame() throws IOException {
        //设置宽高
        this.setSize(400, 400);
        //设置居中
        this.setLocationRelativeTo(null);
        //设置标题
        this.setTitle("扇贝单词");
        //设置标题旁边的logo图标
        this.setIconImage(ImageIO.read(new File("src//image//logo.jpg")));
        //设置置顶
        this.setAlwaysOnTop(true);
        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }
}
